﻿using System.Globalization;
using System.Windows.Data;
using System.Windows.Media;


namespace KxApp.Components
{
    public class SingleBrushConverter : IValueConverter
    {
        public object Convert(object value, Type targetType, object parameter, CultureInfo culture)
        {
            string? myPlcValue;
            if (value == null)
            {
                return true;
            }
            myPlcValue = value?.ToString();


            bool a = bool.TryParse(myPlcValue, out bool myBool);
            if (a)
            {
                if (myBool)
                {
                    return new SolidColorBrush(Colors.Red);
                }
                else
                {
                    return new SolidColorBrush(Colors.Green);
                }
            }
            else
            {
                bool b = float.TryParse(myPlcValue, out float myFloat);
                if (b)
                {
                    if (myFloat > 130)
                    {
                        return new SolidColorBrush(Colors.Red);
                    }
                    else if (myFloat < 130 && myFloat > 60)
                    {
                        return new SolidColorBrush(Colors.Orange);
                    }
                    else
                    {
                        return new SolidColorBrush(Colors.Green);
                    }
                }
            }

            return new SolidColorBrush(Colors.Green);
            ;
        }

        public object ConvertBack(
            object value,
            Type targetType,
            object parameter,
            CultureInfo culture
        )
        {
            throw new NotImplementedException();
        }
    }
}
